<?php
namespace Common\Model;
use Common\Model\CommonModel;
class OrderModel extends CommonModel {

    const ORDER_PENDING = 11;           //待付款
    const ORDER_ACCEPTED = 20;          //待發貨
    const ORDER_CANCELED = 0;           //已取消
    const ORDER_SHIPPED = 30;           //已發貨
    const ORDER_FINISHED = 40;          //已完成
    const ORDER_REFUND_SUCCESS = 50;    //退款成功


    public static $orderStatus = array(
        self::ORDER_ACCEPTED => '待发货',
        self::ORDER_CANCELED => '已取消',
        self::ORDER_FINISHED => '已完成',
        self::ORDER_PENDING  => '待付款',
        self::ORDER_SHIPPED  => '已发货',
        self::ORDER_REFUND_SUCCESS => '退款成功',
    );


    public function getOrderList($cond){
    	if(!$cond){
    		return false;
    	}
    	if(!$cond['page']){
            $res = M('order')
                ->join('ecm_member on ecm_order.buyer_id = ecm_member.user_id')
                ->join('ecm_order_goods on ecm_order.order_id = ecm_order_goods.order_id ')
                ->order($cond['order'])
                ->where($cond['condition'])
                ->field($cond['field'])
                ->select();
            foreach($res as $k=>$v){
                if($v['first_class']){
                    if($v['second_class']){
                        $store_id = $v['second_class'];
                    }else{
                        $store_id = $v['first_class'];
                    }
                }else{
                    $store_id = $v['s_id'];
                }
                $res[$k]['store_name'] = M('store')->where(array("store_id"=>$store_id))->getField('store_name');
                $res[$k]['status_value'] = self::$orderStatus[$v['status']];
            }
        }else{
            $res = M('order')
                ->join('ecm_member on ecm_order.buyer_id = ecm_member.user_id')
                ->join('ecm_order_goods on ecm_order.order_id = ecm_order_goods.order_id ')
                ->where($cond['condition'])
                ->page($cond['page'])
                ->order($cond['order'])
                ->field($cond['field'])
                ->select();
            foreach($res as $k=>$v){
                if($v['first_class']){
                    if($v['second_class']){
                        $store_id = $v['second_class'];
                    }else{
                        $store_id = $v['first_class'];
                    }
                }else{
                    $store_id = $v['s_id'];
                }
                $res[$k]['store_name'] = M('store')->where(array("store_id"=>$store_id))->getField('store_name');
                $res[$k]['status_value'] = self::$orderStatus[$v['status']];
            }
            return $res;
        }
    	return $res;
    }
    public function getSalesCount($cond){
        if(!$cond){
            return false;
        }
        $res = M('order')->field("count(*) as sum,FROM_UNIXTIME(pay_time, '%Y-%m-%d') as date")
            ->group('date')
            ->where($cond)
            ->select();

        return $res;
    }
}